package com.leetcode.LC;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;

public class LC03 {
    public static int lengthOfLongestSubstring(String s) {
        char[] chars = s.toCharArray();
        int i = 0;
        int j = 0;

        Set<Character> strSet = new HashSet<Character>();
        int maxLength = 0;
        int length = 0;
        while (j < chars.length&&i<chars.length) {
            if (!strSet.contains(chars[i])) {
                strSet.add(chars[i++]);
                length++;
            } else {
                while (strSet.contains(chars[i])) {
                    strSet.remove(chars[j]);
                    j = j + 1;
                    length--;
                }
            }
            if (length > maxLength) {
                maxLength = length;
            }
        }
        return maxLength;
    }

    public static void main(String[] args) {
        int maxLength = lengthOfLongestSubstring("abcabcd");
        System.out.println(maxLength);
    }
}
